<?php
declare (strict_types=1);

namespace app\common\controller;

class Util
{
    /**
     * 将图片保存到本地
     */
    public static function base64ImageContent($base64_image_content, $path, $file_name)
    {
        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
            $type = $result[2];
            $new_file = $path . "/";
            if (!file_exists($new_file)) {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                mkdir($new_file, 0777, true);
            }
            $new_file = $new_file . $file_name . ".{$type}";
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {

                return '/' . $new_file;
            } else {

                return false;
            }
        } else {

            return false;
        }
    }

    /**
     * 将图片转换为图片的base64编码
     * $img_file [图片路径]
     */
    public static function imgToBase64($img_file)
    {
        $img_base64 = '';
        if (file_exists($img_file)) {
            $app_img_file = $img_file; // 图片路径
            $img_info = getimagesize($app_img_file); // 取得图片的大小，类型等
            $fp = fopen($app_img_file, "r"); // 图片是否可读权限
            if ($fp) {
                $filesize = filesize($app_img_file);
                $content = fread($fp, $filesize);
                $file_content = chunk_split(base64_encode($content)); // base64编码
                switch ($img_info[2]) {           //判读图片类型
                    case 1:
                        $img_type = "gif";
                        break;
                    case 2:
                        $img_type = "jpg";
                        break;
                    case 3:
                        $img_type = "png";
                        break;
                }
                $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
            }
            fclose($fp);
        }

        return $img_base64; //返回图片的base64
    }

    /**
     * 微信红包算法(可以设置最大金额和最小金额)
     */
    public static function getRedPackage($money, $num, $min, $max)
    {
        $data = [];
        if ($min * $num > $money) {
            return [];
        }
        if ($max * $num < $money) {
            return [];
        }
        while ($num >= 1) {
            $num--;
            $kmix = max($min, $money - $num * $max);
            $kmax = min($max, $money - $num * $min);
            $kAvg = $money / ($num + 1);
            //获取最大值和最小值的距离之间的最小值
            $kDis = min($kAvg - $kmix, $kmax - $kAvg);
            //获取0到1之间的随机数与距离最小值相乘得出浮动区间，这使得浮动区间不会超出范围
            $r = ((float)(rand(1, 10000) / 10000) - 0.5) * $kDis * 2;
            $k = round($kAvg + $r, 2);
            $money -= $k;
            $data[] = $k;
        }

        return $data;
    }

    /**
     * 微信红包算法/递推思想
     * $a金额 $b人数 $min每人最小金额
     */
    public static function redenvelopes($a, $b, $min = '0.01')
    {
        $arr = [];
        for ($i = 1; $i < $b + 1; $i++) {
            if ($i == $b) {
                $arr[] = $a;
            } else {
                $max = round($a / ($b + 1 - $i) * 2, 2);
                $money = round(mt_rand($min * 100, $max * 100) / 100, 2);
                $a = round($a - $money, 2);
                $arr[] = $money;
            }
        }

        return $arr;
    }

    /**
     * 近某天
     */
    public static function getDay($day)
    {
        $time = time();
        //组合数据
        $arr = [];
        for ($i = 1; $i <= $day; $i++) {
            $real_day = $i - $day;
            $k = date('Y-m-d', strtotime('+' . $real_day . ' days', $time));
            $key = date('m-d', strtotime($k));
            $t['start'] = $k . ' 00:00:00';
            $t['end'] = $k . ' 23:59:59';
            $arr[$key] = $t;
        }

        return $arr;
    }

    /**
     * 计算两个日期相隔天数
     */
    public static function dateApart($start_date, $end_date)
    {
        list($start_year, $start_month, $start_day) = explode('-', $start_date);
        $start = mktime(0, 0, 0, (int)$start_month, (int)$start_day, (int)$start_year);
        list($end_year, $end_month, $end_day) = explode('-', $end_date);
        $end = mktime(0, 0, 0, (int)$end_month, (int)$end_day, (int)$end_year);

        return ($end - $start) / (3600 * 24);
    }
}